package com.ruoyi.exapmle.service.impl;

import java.util.List;
import java.util.Map;

import javax.annotation.Resource;

import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Propagation;
import org.springframework.transaction.annotation.Transactional;

import com.ruoyi.exapmle.mapper.ExamplePicMapper;
import com.ruoyi.exapmle.domain.ExamplePic;
import com.ruoyi.exapmle.service.IExamplePicService;
import com.ruoyi.oss.config.OssConfig;
import com.basic.service.impl.BaseServiceImpl;
import com.common.util.ListConvertMap;

/**
 * 案例图片Service业务层处理
 * 
 * @author 王强
 * @date 2024-01-22
 */
@Service
public class ExamplePicServiceImpl extends BaseServiceImpl<Long, ExamplePic> implements IExamplePicService 
{
	OssConfig config;

	@Resource
	public void setConfig(OssConfig config) {
		this.config = config;
	}
	
    @Override
	protected ExamplePic[] getEntityObject(int size) {
		// TODO Auto-generated method stub
		return new ExamplePic[size];
	}

	@Override
	@Transactional(propagation = Propagation.MANDATORY)
	public int batchInsert(List<ExamplePic> data, int id) {
		// TODO Auto-generated method stub
		return super.batchInsert(data, id);
	}

	@Override
	protected Class<ExamplePic> getEntityClass() {
		// TODO Auto-generated method stub
		return ExamplePic.class;
	}

	public ExamplePicServiceImpl(ExamplePicMapper baseMapper) {
		super(baseMapper);
		// TODO Auto-generated constructor stub
	}
    /**
     * 查询案例图片
     * 
     * @param id 案例图片主键
     * @return 案例图片
     */
    @Override
    public ExamplePic selectById(Integer id)
    {
        return baseMapper.selectById(id);
    }
    
    protected void afterParseBatchInsert(List<ExamplePic> data, int id) {
    	for (ExamplePic examplePic : data) {
			examplePic.setExampleId(id);
			common(examplePic);
		}
    }
    
    
    private void common(ExamplePic news) {

		String pic = news.getPic();

		if (null == pic) {
			return;
		}

		String prefix = (config.getURL());

		pic = pic.replaceAll("http://", "").replaceAll("https://", "").replaceAll(prefix, "");

		news.setPic(pic);
	}
    
    

    /**
     * 查询案例图片列表
     * 
     * @param examplePic 案例图片
     * @return 案例图片
     */
    @Override
    public List<ExamplePic> selectList(ExamplePic examplePic)
    {
    	List<ExamplePic> selectList = baseMapper.selectList(examplePic);

		if (0 == selectList.size()) {
			return selectList;
		}

		String prefix = "http://".concat(config.getURL());

		for (ExamplePic item : selectList) {

			item.setPic(prefix.concat(item.getPic()));

		}
    	
        return selectList;
    }

    /**
     * 新增案例图片
     * 
     * @param examplePic 案例图片
     * @return 结果
     */
    @Override
    public int insert(ExamplePic examplePic)
    {
        return baseMapper.insert(examplePic);
    }

    /**
     * 修改案例图片
     * 
     * @param examplePic 案例图片
     * @return 结果
     */
    @Override
    public int update(ExamplePic examplePic)
    {
        return baseMapper.update(examplePic);
    }

    /**
     * 批量删除案例图片
     * 
     * @param ids 需要删除的案例图片主键
     * @return 结果
     */
    @Override
    public int deleteByIds(Integer[] ids)
    {
        return baseMapper.deleteByIds(ids);
    }

    /**
     * 删除案例图片信息
     * 
     * @param id 案例图片主键
     * @return 结果
     */
    @Override
    public int deleteById(Integer id)
    {
        return baseMapper.deleteById(id);
    }

	@Override
	@Transactional(propagation = Propagation.MANDATORY)
	public void parseUpdate(List<ExamplePic> examplePics) {
		
//		ExamplePic examplePic = new ExamplePic();
		
		if (null == examplePics) {
			return;
		}
		
		Integer exampleId = examplePics.get(0).getExampleId();
		
		if (null == exampleId) {
			throw new IllegalArgumentException("案例id不能为空");
		}
		
		ExamplePicMapper examplePicMapper =  this.getBaseMapper();
		
		examplePicMapper.deleteByExampleId(exampleId);
		
		this.batchInsert(examplePics, exampleId);
		
//		examplePic.setExampleId(exampleId);
//		
//		List<ExamplePic> selectList = this.selectList(examplePic);
//		
//		if (0 == selectList.size()) {
//			this.batchInsert(examplePics);
//			return;
//		}
//		
//		
//		this.afterParseBatchInsert(examplePics, exampleId);
//		
//		Map<String, ExamplePic> listToMap = ListConvertMap.listToMap(examplePics, ExamplePic::getPic);
//		
//		
//		for (ExamplePic item : selectList) {
//			
//			ExamplePic res = listToMap.get(item.getPic());
//			
//			if (null == res) {
//				
//			}
//			
//		}
//		
	}
}
